package com.bjqg.bjqg01.bjqg.activity.longrange.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjqg.bjqg01.bjqg.R;
import com.bjqg.bjqg01.bjqg.activity.address.adapter.AddressAdapter;
import com.bjqg.bjqg01.bjqg.activity.longrange.bean.ChoiceBean;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;

/**
 * Created by bjqg01 on 2018/9/12.
 */

public class ChoiceAdapter extends BaseAdapter {
    List<ChoiceBean> list;
    Context context;
    private onItemClickListener onItemClickListener;


    public interface onItemClickListener {
        void onItem(int position);
    }

    public void setOnItemClickListener(onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    public ChoiceAdapter(Context context, List<ChoiceBean> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View view, ViewGroup parent) {

        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chooice, null);
            holder = new ViewHolder(view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        if (list.get(position).isclick()) {
            holder.cb.setChecked(true);
        } else {
            holder.cb.setChecked(false);
        }
        holder.tv.setText(list.get(position).getName());

        holder.llfather.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItem(position);
                }
            }
        });

        return view;
    }

    static class ViewHolder {
        CheckBox cb;
        TextView tv;
        AutoLinearLayout llfather;

        ViewHolder(View view) {
            cb = (CheckBox) view.findViewById(R.id.cb);
            tv = (TextView) view.findViewById(R.id.tv_name);
            llfather = (AutoLinearLayout) view.findViewById(R.id.ll_item_father);
        }
    }
}
